<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>004 数组去重</title>
</head>
<body>

  <h1>004 数组去重</h1>
  <p>任意长度的字符串数组，去除其中重复内容。</p>
  <p>如 ["a","b","a","c","d","c"]，返回新数组 ["a", "b", "c", "d"]</p>

  <form id="myform" method="get">
    <input type="text" name="arr" value="%%abacdc" placeholder="请随机输入内容...">
    <input type="submit" value="去重">
  </form>

  <script>
    document.getElementById("myform").addEventListener("submit", function(e) {

      //阻止默认提交行为.
      e.preventDefault();

      var myArr = this.arr.value;

      var targetArr = myArr.split("");
      var resultArr = [];
      var isExist = false;

      if( myArr.length <= 0 ) {
        alert("请随机输入内容！");
      }

      for(var i = 0, len = targetArr.length; i < len; i++) {
        isExist = false;
        for(var j = 0, size = resultArr.length; j < size; j++) {
          if(targetArr[i] === resultArr[j]) {
            isExist = true;
          }
        }
        if(!isExist) {
          resultArr.push(targetArr[i]);
        }
      }

      alert(resultArr);
      alert(targetArr.filter((n, i) => targetArr.includes(n) && targetArr.indexOf(n) === i));

    }, false);

  </script>

</body>
</html>
